package com.zis.auction.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.zis.auction.model.AuctUser;
import com.zis.auction.service.IAuctUserInter;
import com.zis.auth.service.ISercretInter;
import com.zis.base.impl.BaseInterImpl;
import com.zis.core.ResponseEntity;
import com.zis.wechat.model.TUser;
import com.zis.wechat.service.ITUserInter;
import org.beetl.sql.core.SQLManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * Create by wangshen 2018/1/2
 */
@Service
@Transactional("txManager")
public class AuctUserInterImpl extends BaseInterImpl<AuctUser> implements IAuctUserInter {

    @Autowired
    private SQLManager sqlManager;

    @Autowired
    private ISercretInter sercretInter;

    @Autowired
    private ITUserInter itUserInter;

    @Override
    public String insert(AuctUser auctUser) throws Exception {
        return null;
    }

    @Override
    public String update(AuctUser auctUser) throws Exception {
        return null;
    }

    @Override
    public String delete(String id) throws Exception {
        return null;
    }

    @Override
    public AuctUser selectById(String id) throws Exception {
        return null;
    }

    @Override
    public List<AuctUser> findByCondition(AuctUser auctUser) throws Exception {
        return null;
    }

    /**
     * 用户信息认证
     * @param params
     * @return
     */
    @Override
    public String userAuth(JSONObject params) throws Exception {
        if (params == null) {
            return ResponseEntity.createNULLJsonResponse("认证信息为空");
        }
        if (!params.containsKey("realName") || params.getString("realName").equals("")) {
            return ResponseEntity.

                    createNULLJsonResponse("请输入用户姓名");
        }
        if (!params.containsKey("telephone") || params.getString("telephone").equals("")) {
            return ResponseEntity.createNULLJsonResponse("请输入手机号码");
        }
        if (!params.containsKey("authCode") || params.getString("authCode").equals("")) {
            return ResponseEntity.createNULLJsonResponse("请输入验证码");
        }
        if (!params.containsKey("tuId") || params.getString("tuId").equals("")) {
            return ResponseEntity.createNULLJsonResponse("请从微信菜单点击进入");
        }
        String realName = params.getString("realName");
        String idNumber = params.containsKey("idNumber") == true ? params.getString("idNumber") : "";
        String telephone = params.getString("telephone");
        String authCode = params.getString("authCode");
        String tuId = params.getString("tuId");
        String authResult = sercretInter.verifySmsCode(telephone, authCode);
        if (!authResult.equals("ok")) {
            return ResponseEntity.createErrorJsonResponse(authResult);
        }
        AuctUser auctUser = new AuctUser();
        auctUser.setRealName(realName);
        auctUser.setIdNumber(idNumber);
        auctUser.setTelephone(telephone);
        auctUser.setAuthState("1");
        auctUser.setAuthDate(new Date());
        auctUser.setTuId(tuId);
        int num = sqlManager.insert(auctUser);
        if (num > 0) {
            // 更新tuser
            TUser tUser = new TUser();
            tUser.setTuId(tuId);
            tUser.setAuthBuss("1");
            itUserInter.update(tUser);
            // 认证成功返回主键ID
            return ResponseEntity.createNormalJsonResponse(auctUser.getAusId());
        }
        return ResponseEntity.createErrorJsonResponse("认证失败");
    }

    @Override
    public String getAuctUser(String tuId) throws Exception {
        AuctUser params = new AuctUser();
        params.setTuId(tuId);
        List<AuctUser> userList = sqlManager.template(params);
        if (userList!=null&&userList.size()>0) {
            AuctUser user = userList.get(0);
            JSONObject userJSON = new JSONObject();
            userJSON.put("ausId", user.getAusId());
            userJSON.put("realName", user.getRealName());
            userJSON.put("telephone", user.getTelephone());
            return ResponseEntity.createNormalJsonResponse(userJSON);
        }
        return ResponseEntity.createErrorJsonResponse("未获取到用户认证信息");
    }

    @Override
    public AuctUser getAuctUserObject(String tuId) throws Exception {
        AuctUser params = new AuctUser();
        params.setTuId(tuId);
        List<AuctUser> userList = sqlManager.template(params);
        if (userList!=null&&userList.size()>0) {
            return userList.get(0);
        }
        return null;
    }
}
